package com.winit.schedule.job.tracker.event;

import com.winit.schedule.core.event.EventHandler;
import com.winit.schedule.core.repository.entity.QuartzLogEntity;
import com.winit.schedule.job.tracker.JobTrackerContext;
import com.winit.schedule.job.tracker.repository.ScheduleDataManager;

public class QuartzEventHandler implements EventHandler<QuartzEvent> {

    private ScheduleDataManager scheduleDataManager;

    public QuartzEventHandler(JobTrackerContext jobTrackerContext){
        this.scheduleDataManager = jobTrackerContext.getScheduleDataManager();
    }

    @Override
    public void handle(QuartzEvent event) {
        QuartzEventType type = event.getType();
        if (QuartzEventType.UPDATE_SCHEDULER_INFO.equals(type)) {
            // 更新quartz作业信息
            scheduleDataManager.updateQuartzScheduler(event.getSchedulerEntities());
        } else if (QuartzEventType.QUARTZ_LOG.equals(type)) {
            QuartzLogEntity logEntity = event.getLogEntity();
            // 记录日志
            scheduleDataManager.logger(logEntity);

            // 清除操作命令
            // if
            // (QuartzLogType.COMMAND_EXE_ERROR.equals(logEntity.getLogType())
            // ||
            // QuartzLogType.COMMAND_EXE_SUCCESS.equals(logEntity.getLogType()))
            // {
            // scheduleDataManager.cleanQuartzCommand(logEntity.getTaskTrackerUuid());
            // }
        } else if (QuartzEventType.CLEAN_COMMAND.equals(type)) {
            // 清除操作命令
            scheduleDataManager.cleanQuartzCommand(event.getTaskTrackerUuid());
        }
    }

    public ScheduleDataManager getScheduleDataManager() {
        return scheduleDataManager;
    }

}
